
 //        Double d = (i * i) * 0.1;
 //         n = d.intValue();


import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.util.ArrayList;
import java.util.List;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.swing.JOptionPane;

public class NewJFrame extends javax.swing.JFrame {
   public int n;
   public int a = 40;
   public int b = 290;
   List<Point> lPuntos = new ArrayList<Point>();
   boolean flag = false;
    
    public NewJFrame() {
        initComponents();
    }
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>

    @Override
    public void paint(Graphics g){
        super.paint(g);
        g.setColor(Color.WHITE);
        g.fillRect(40, 40, 850, 500);
        g.setColor(Color.black);
        g.drawLine(40, 290, 890, 290); // Eje de coordenadas x
        g.drawLine(465, 40, 465, 540); // Eje de coordenadas y

        for(int i = 0; i<=85; i++)       // Pinta intervalos en X
           g.drawLine((i*10)+40, 288, (i*10)+40, 292);

        for(int i = 0; i<=50; i++)     // Pinta los intervalos en Y
          g.drawLine(462, (i*10)+40, 467, (i*10)+40);

        g.setColor(Color.blue);

 //       System.out.println(lPuntos.size());
       if(flag){
       for(int i = 0; i <= lPuntos.size(); i++){
//(10 * (lPuntos.get(i).getY()) *(-1))
           System.out.println(lPuntos.get(i));
//              if ((n >= -25)&&(n <= 25 )){
        //          g.drawOval(a, b+1, 5, 5);
//              }
//            a += 10;
        //}
       }
       }

    }

    public void repaint(){
       super.repaint();
    }


    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jButton1.setText("Graficar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(59, 59, 59)
                .addComponent(jLabel1)
                .addGap(290, 290, 290)
                .addComponent(jButton1)
                .addContainerGap(475, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(502, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jLabel1))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed

       ScriptEngineManager manager = new ScriptEngineManager();
       ScriptEngine engine = manager.getEngineByName("js");

   //    int a = 5;
  //     engine.put("x", a);

       String formula = JOptionPane.showInputDialog("Escribe la Ecuacion:");

       try {
           for(int i = -42; i <= 42; i++){
               engine.put("x", i);
               Double operation = (Double) engine.eval(formula);
               n = operation.intValue();
               lPuntos.add(new Point(i, n));
           }

        } catch (Exception e) {
            e.printStackTrace();
        }
       flag = true;
       repaint();
    }//GEN-LAST:event_jButton1ActionPerformed

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new NewJFrame().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    // End of variables declaration//GEN-END:variables

}
